<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		hasEditor,
		getEditorTitle,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				sourceIdCacheKey: 'hr-train-trainrecord-sourceid-cache',
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			findListByTrainNoticeShow(){
				let _that = this
				_that.om.request(
					"trainRecord/findListByTrainNoticeShow",
					{
						trainNoticeShowId:_that.$refs.autoform.formdata.sourceId
					}
				).then(res=>{
					if(res.succeed == true){
						let itemlist = res.value.itemList;
						let userlist = res.value.userList;
						for(let item of itemlist){
							item.itemUserScore = userlist.map(e=>e.evaluateUserName +": 未填写").join('\n')
							item.userList =  userlist.map(e=>{return {"evaluateUserId":e.evaluateUserId}})
						}
						_that.subEditorObj["title3"].workList = itemlist
					}
				})
			},
			clearSource(){
				this.$refs.autoform.clear("title1","sourceId")
				this.$refs.autoform.clear("title1","sourceCode")
				this.$refs.autoform.clear("title1","sourceZuserId")
				this.$refs.autoform.clear("title1","sourceZuserName")
				this.$refs.autoform.clear("title1","sourceZtime")
				this.$refs.autoform.clear("title1","trainPlanCode")
				this.$refs.autoform.clear("title1","projectTypeId")
				this.$refs.autoform.clear("title1","projectTypeName")
				this.$refs.autoform.clear("title1","projectName")
				this.$refs.autoform.clear("title1","timeNum")
				this.$refs.autoform.clear("title1","trainTypeId")
				this.$refs.autoform.clear("title1","trainTypeName")
				this.$refs.autoform.clear("title1","responsibleUserId")
				this.$refs.autoform.clear("title1","responsibleUserName")
				this.$refs.autoform.clear("title1","startDate")
				this.$refs.autoform.clear("title1","endDate")
				this.$refs.autoform.clear("title1","partakeUserNum")
				this.$refs.autoform.clear("title1","lessonNumber")
				this.$refs.autoform.clear("title1","actualTimeNum")
				this.$refs.autoform.clear("title1","trainEvaluationTypeId")
				this.$refs.autoform.clear("title1","trainEvaluationTypeName")
			},
			initLoad(options){
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"trainRecord/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'), mainData);
					_that.$nextTick(function(){
						// _that.$refs.autoform.initZposId()
						if(res[0].length == 1){
						 	_that.$refs.autoform.initSelectIndex('topinfo', 'zposId')
						 	_that.$set(_that.formdata, 'zposId', res[0].id)
						 	_that.$set(_that.formdata, 'zposName', res[0].posname)
						}
					})
					
					changeEditor("title1","sourceId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other:JSON.stringify({
								oldValue: ""
							}),
							namefield:'code',
							notefield:'projectName',
							extrafield:'userName',
							searchfield:"q",
							initload:true,
							url:'trainRecord/likeSerchTrainNoticeShow',
							cachename:_that.sourceIdCacheKey,
							placeholder:"请输入通知单编码"
						}
					},null,mainData)
					
					if(_that.subEditorObj["title3"]){
						let att = {
							showAdd:false,
							showDel:false
						}
						let configData = _that.subConfigData["title3"]
						if(hasEditor("title3","itemName",configData)){
							att.titleTemplate = "<%= itemName || '未填写'%>"
						}
						if(hasEditor("title3","standardScore",configData)){
							let title  = getEditorTitle("title3","standardScore",configData)
							att.subtitle1Template =title+ ":<%= standardScore || '未填写'%>"
						}
						if(hasEditor("title3","itemUserScore",configData)){
							let title = getEditorTitle("title3","itemUserScore",configData)
							att.subtitle2Template = title + ":<%= itemUserScore || '未填写'%>"
						}
						if(hasEditor("title3","singleScore",configData)){
							let title = getEditorTitle("title3","singleScore",configData)
							att.subtitle3Template = title + ":<%= singleScore || '未填写' %>"
						}
						changeEditor("title3","dataStr",att,mainData)
					}
					
					if(fileId){
						let fileIdArr = getFormFields(mainData)
						fileIdArr.push("id")
						for(let index in res[1].value){
							if(fileIdArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
								_that.$set(_that.formdata,index,res[1].value[index])
							}
						}

						let itemlist = _that._.cloneDeep(res[1].value.itemList);
						let userlist = _that._.cloneDeep(res[1].value.userList);
						for(let item of itemlist){
							item.itemUserScore = userlist.map(e=>e.evaluateUserName +": 未填写").join('\n')
							item.userList =  userlist.map(e=>{return {"evaluateUserId":e.evaluateUserId}})
						}
						_that.subEditorObj["title3"].workList = itemlist
						
						_that.$nextTick(function(){
							_that.$refs.autoform.changeEditor("topinfo","zposId","type","labelbox")
							_that.$refs.autoform.changeEditor("title1","sourceId","type","labelbox")
						})
					}
				},options)
			},
			formSubmit(){
				let _that = this 
				if(_that.edit){
					_that.om.formSave("trainRecord/doUpdate",_that.formdata,_that)
				}else if(_that.checkedit){
					// _that.om.formSave("trainRecord/doReviewUpdate",_that.formdata,_that)
				}else{
					_that.om.formSave("trainRecord/doAdd",_that.formdata,_that)
				}
			},
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			uni.getStorage({
				key: _that.sourceIdCacheKey,
				success: function(res) {
					if(res.data){
						let d = res.data
						var formdata  = _that.$refs.autoform.formdata
						formdata.sourceId = d.id
						formdata.sourceCode = d.code
						formdata.sourceZuserId = d.zuserId
						formdata.sourceZuserName = d.zuserName
						formdata.sourceZtime = d.ztime
						formdata.trainPlanCode = d.orderNum
						formdata.projectTypeId = d.projectTypeId
						formdata.projectTypeName = d.projectTypeName
						formdata.projectName = d.projectName
						formdata.timeNum = d.timeNum
						formdata.trainTypeId = d.trainTypeId
						formdata.trainTypeName = d.trainTypeName
						formdata.responsibleUserId = d.userId
						formdata.responsibleUserName = d.username
						formdata.startDate = d.startDate
						formdata.endDate = d.endDate
						formdata.partakeUserNum = d.partakeUserNum
						formdata.lessonNumber = d.lessonNumber
						formdata.actualTimeNum = d.actualTimeNum
						formdata.trainEvaluationTypeId = d.trainEvaluationTypeId
						formdata.trainEvaluationTypeName = d.trainEvaluationTypeName		
						if(_that.subEditorObj["title3"]){
							_that.subEditorObj["title3"].workList = []
						}
						_that.findListByTrainNoticeShow()
						uni.removeStorage({
							key: _that.sourceIdCacheKey
						});
					}
				},
			});
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
